import 'package:flutter/material.dart';
import 'package:flutter_wisave/widgets/AppColors.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

class deviceLinePie extends StatelessWidget {
  const deviceLinePie({
    super.key,
    required this.date,
    required this.numberCount,
    required this.dataValue01,
    required this.dataValue02,
    required this.dataValue03,
    required this.dataValue04,
    required this.dataValue05,
    required this.dataValue06,
  });

  final String date;
  final int numberCount;
  final double dataValue01;
  final double dataValue02;
  final double dataValue03;
  final double dataValue04;
  final double dataValue05;
  final double dataValue06;

  List<int> findFirstAndLastPositiveIndex() {
    List<double> dataValues = [
      dataValue01,
      dataValue02,
      dataValue03,
      dataValue04,
      dataValue05,
      dataValue06
    ];

    int firstIndex = -1;
    int lastIndex = -1;

    for (int i = 0; i < dataValues.length; i++) {
      if (dataValues[i] > 0) {
        if (firstIndex == -1) {
          firstIndex = i; // 记录第一个大于0的下标
        }
        lastIndex = i; // 更新最后一个大于0的下标
      }
    }

    return [firstIndex, lastIndex];
  }

  Widget buildDataBar(double dataValue, int numberCount, Color color,
      bool islBorder, bool isrBorder, BuildContext context) {
    final widthParent = MediaQuery.of(context).size.width - 64;
    return SizedBox(
      width: widthParent * (dataValue / numberCount),
      child: Container(
        width: double.infinity,
        child: Column(
          children: [
            Row(
              children: [
                Expanded(child: SizedBox()),
                TDText(
                  dataValue.toString(),
                  font: TDTheme.of(context).fontBodySmall,
                  textColor: AppColors.textSecondary,
                ),
              ],
            ),
            SizedBox(
              height: 10,
              width: double.infinity,
              child: Container(
                decoration: BoxDecoration(
                  color: color,
                  borderRadius: BorderRadius.only(
                    topRight:
                        isrBorder ? Radius.circular(5) : Radius.zero, // 设置右上角圆角
                    bottomRight:
                        isrBorder ? Radius.circular(5) : Radius.zero, // 设置右下角圆角
                    topLeft:
                        islBorder ? Radius.circular(5) : Radius.zero, // 设置左上角圆角
                    bottomLeft:
                        islBorder ? Radius.circular(5) : Radius.zero, // 设置左下角圆角
                  ),
                ),
              ),
            ),
            Row(
              children: [
                Expanded(child: SizedBox()),
                TDText(
                  (((dataValue / numberCount) * 100).toString() + '%'),
                  font: TDTheme.of(context).fontBodySmall,
                  textColor: AppColors.textSecondary,
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        child: Column(
      children: [
        const SizedBox(height: 10),
        Row(
          children: [
            Text(date),
            Expanded(child: SizedBox()), // 使用Expanded来占据剩余空间
            Text(numberCount.toString() + '次'),
          ],
        ),
        Row(
          children: [
            if (dataValue01 > 0)
              buildDataBar(
                  dataValue01,
                  numberCount,
                  Color(0xFF2AB6F7),
                  findFirstAndLastPositiveIndex()[0] == 0,
                  findFirstAndLastPositiveIndex()[1] == 0,
                  context),
            if (dataValue02 > 0)
              buildDataBar(
                  dataValue02,
                  numberCount,
                  Color(0xFFFCAA3F),
                  findFirstAndLastPositiveIndex()[0] == 1,
                  findFirstAndLastPositiveIndex()[1] == 1,
                  context),
            if (dataValue03 > 0)
              buildDataBar(
                  dataValue03,
                  numberCount,
                  Color(0xFF53E0B1),
                  findFirstAndLastPositiveIndex()[0] == 2,
                  findFirstAndLastPositiveIndex()[1] == 2,
                  context),
            if (dataValue04 > 0)
              buildDataBar(
                  dataValue04,
                  numberCount,
                  Color(0xFFFCAA3F),
                  findFirstAndLastPositiveIndex()[0] == 3,
                  findFirstAndLastPositiveIndex()[1] == 3,
                  context),
            if (dataValue05 > 0)
              buildDataBar(
                  dataValue05,
                  numberCount,
                  Color(0xFFEF6B9C),
                  findFirstAndLastPositiveIndex()[0] == 4,
                  findFirstAndLastPositiveIndex()[1] == 4,
                  context),
            if (dataValue06 > 0)
              buildDataBar(
                  dataValue06,
                  numberCount,
                  Color(0xFFFD5864),
                  findFirstAndLastPositiveIndex()[0] == 5,
                  findFirstAndLastPositiveIndex()[1] == 5,
                  context),
          ],
        ),
      ],
    ));
  }
}
